import router from './router'
import store from './store'
import NProgress from 'nprogress' //NProgress是封装的进度条，基本不用动
import 'nprogress/nprogress.css'

NProgress.configure({ showSpinner: false });

// setToken('Admin-Token')//初始化token
// var _uid = this.get('txb_authId');

//路由白名单列表，把路由添加到这个数组，不用登陆也可以访问
const whiteList = ['/login'];
router.beforeEach((to, from, next) => {
  // 请求路由时进度条开始
  NProgress.start();
  if (store.getters.topbarRouters.length === 0) {
    store.dispatch('GenerateRoutes').then(accessRoutes => {
      console.log(accessRoutes)
      // 根据roles权限生成可访问的路由表
      router.addRoutes(accessRoutes) // 动态添加可访问路由表
      next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
    }).catch(err => {
      next()//关闭登录拦截
    })
  } else {
    next()
  }


});

//每次请求结束后都需要关闭进度条
router.afterEach(() => {
  NProgress.done()
});
